package com.mytime.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class Date {

	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	private String completeHour;
	private String completeDate;
	private String date;
	
	private int day;
	private int month;
	private int year;
	private int hour;
	private int minute;
	
	public Date() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
		
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		this.completeDate = sdf.format(calendar.getTime());
		
		String[] completeDateInfo = this.completeDate.split(" ");
		this.date = completeDateInfo[0];
		this.completeHour = completeDateInfo[1];
		
		String[] hourInfo = this.completeHour.split(":"); 
		this.hour = Integer.parseInt(hourInfo[0]);
		this.minute = Integer.parseInt(hourInfo[1]);
		
		String[] dateInfo = this.date.split("-");
		this.year = Integer.parseInt(dateInfo[0]);
		this.month = Integer.parseInt(dateInfo[1]);
		this.day = Integer.parseInt(dateInfo[2]);
	}
	
	/**
	 * Gets the Date including the time.
	 * **/
	public String getDate() {
		return this.completeDate;
	}
	
	/**
	 * Returns the day.
	 * **/
	public int getDay() {
		return this.day;
	}
	
	/**
	 * Returns the month.
	 * **/
	public int getMonth() {
		return this.month;
	}

	/**
	 * Returns the year.
	 * **/
	public int getYear() {
		return this.year;
	}
	
	/**
	 * Returns the hour.
	 * **/
	public int getHour() {
		return this.hour;
	}

	/**
	 * Compares two Date objects.
	 * It returns 0 if the two dates are equal, 1 if the first Date is greater 
	 * and -1 if it is smaller. 
	 * **/
	public static int compareDate(Date d1, Date d2) {
		int result = 0;
		
		if (compare(d1.getYear(), d2.year) != 0) {
			result = compare(d1.getYear(), d2.year);
			
		} else if (compare(d1.getMonth(), d2.month) != 0) {
			result = compare(d1.getMonth(), d2.month);
			
		} else if (compare(d1.getDay(), d2.day) != 0) {
			result = compare(d1.getDay(), d2.day);
		
		} else if (compare(d1.getHour(), d2.hour) != 0) {
			result = compare(d1.getDay(), d2.hour);
			
		} else if (compare(d1.getDay(), d2.minute) != 0) {
			result = compare(d1.getDay(), d2.minute);
			
		}
		
		return result;
	}

	/**
	 * Compare two integer values.
	 * If the values are equal, it returns 0. If value1 is greater than value2, it returns 1.
	 * If value2 is greater than value1, it returns -1. 
	 * **/
	private static int compare(int value1, int value2) {
		int result = 0;
		
		if (value1 > value2) {
			result = 1;
		} else if (value1 < value2) {
			result = -1;
		}
		 
		return result;
	}
}